package com.xzlf.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xzlf.domain.JobTask;
import com.xzlf.domain.TaskRate;
import com.xzlf.service.JobTaskService;
import com.xzlf.service.NbsScheduled;
import com.xzlf.service.TFileService;
import com.xzlf.service.TaskRateService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.scheduling.support.PeriodicTrigger;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Configuration
@Slf4j
public class ScheduledConfig02 implements SchedulingConfigurer {

    private static final SimpleDateFormat SIMPLE_DATE_FORMAT= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private Map<String, NbsScheduled> taskMap;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private TFileService tFileService;

    @Autowired
    private JobTaskService jobTaskService;

    @Value("${task.lock.key1}")
    private String lockKey1;

    @Value("${task.lock.key2}")
    private String lockKey2;

    @Value("${task.lock.key3}")
    private String lockKey3;

    @Value("${task.lock.value1}")
    private String lockValue1;

    @Value("${task.lock.value2}")
    private String lockValue2;

    @Value("${server.name}")
    private String serverName;

    @Value("${job.execute.enable}")
    private boolean isExecute;

    @Autowired
    private TaskRateService taskRateService;


    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        for (String taskKey : taskMap.keySet()) {
            JobTask jobTask = jobTaskService.getOne(new LambdaQueryWrapper<JobTask>()
                    .eq(JobTask::getJobName, taskKey));
            taskRegistrar.addTriggerTask(
                    () -> {
                       log.info("我是：{}，开始:{}", serverName, jobTask.getJobDesc());
                        try {
                            TimeUnit.SECONDS.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        log.info("我是：{}，完成:{}，完成时间", serverName, jobTask.getJobDesc(),SIMPLE_DATE_FORMAT.format(new Date()));
                    },
                    new Trigger() {
                        @Override
                        public Date nextExecutionTime(TriggerContext triggerContext) {
                            String jobName = jobTask.getJobName();
                            Integer time = taskRateService.getOne(
                                    new LambdaQueryWrapper<TaskRate>()
                                            .eq(TaskRate::getTaskName, jobName))
                                    .getTime();
                            PeriodicTrigger periodicTrigger = new PeriodicTrigger(time, TimeUnit.SECONDS);
//                            periodicTrigger.setFixedRate(true);
                            return periodicTrigger.nextExecutionTime(triggerContext);
                        }
                    });
        }
    }
}
